package org.example.y24.m11;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class WordGuessingGame {
    private static String word;
    private static int[] scores = {1,2,3,4,4,3,2,1};
    private static  String filePath = "F:/";
    public static void main(String[] args) {
        WordGuessingGame game = new WordGuessingGame();
        Scanner s = new Scanner(System.in);
        while (true){
            game.menu();
            char choice = s.next().charAt(0);
            if (choice == 's' || choice == 'S'){
                try {
                    word = game.readWordsFromFile();
                    System.out.println(word);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                game.startGame();
            }else if (choice == 'h' || choice == 'H'){
                game.help();
            } else if (choice == 'q' || choice == 'Q') {
                System.exit(0);
            }else{
                System.out.println("输入错误请重新输入");
            }
        }

    }
    public void menu(){
        System.out.println("********************************************");
        System.out.println("Welcome to the Criss-Cross Multi-Step Word Guessing Game!");
        System.out.println(" Enter an input action. Choices are: ");
        System.out.println("s. Start game");
        System.out.println("h. get help on game rules");
        System.out.println("q. Quit game");
        System.out.println("********************************************");
    }
    public void help(){
        InputStream inputStream = null;
        List<String> gz;
        try {
            gz = readFile("gz.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (String s : gz) {
            System.out.println(s);
        }
    }
    public void startGame(){
        int score = 0;
        Scanner s = new Scanner(System.in);
        for(int i = 0;i < word.length();i++){
            System.out.println("请输入第"+(i+1)+"个字母");
            char input = s.next().charAt(0);
            if (input == word.charAt(i)){
                score += scores[i];
                System.out.print("恭喜你猜对了！");
            }else{
                System.out.print("你输入的字母是："+input+"，正确答案是："+word.charAt(i));
            }
            System.out.println("当前得分为："+score);
        }
        if(score >= 12){
            System.out.println("学霸");
        }else {
            System.out.println("加油");
        }
    }
    //通过IO读取txt文件中单词
    public String readWordsFromFile() throws IOException {
        List<String> wordList = readFile("WORDS_FIXED.txt");
        return wordList.get((int)(Math.random()*wordList.size()));
    }
    public List<String> readFile(String fileName) throws IOException {
        InputStream inputStream = new FileInputStream(filePath + fileName);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        List<String> list = new ArrayList<>();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
        }
        bufferedReader.close();
        return list;
    }
}
